<template>
  <div>
    <Chart :cdata="cdata"/>
  </div>
</template>

<script>
import Chart from './chart.vue'
import {getsimplejson} from "@/api";
// import {timeUtil} from "@/utils/nodeId";

export default {
  data() {
    return {
      cdata: {
        category: [],
        lineData: [],
        barData: [],
        rateData: []
      }
    };
  },
  components: {
    Chart,
  },
  mounted() {
    this.setData();
  },
  methods: {
    // 根据自己的业务情况修改
    setData() {

      this.cdata.category = this.get_date(30)
      console.log('this.cdata.category=>', this.cdata.category)
      const startTimeArr = this.cdata.category[0].split('-')
      const startTime = startTimeArr[0] + '-' + startTimeArr[1] + '-' + (Number(startTimeArr[2]) - 1)
      const body = {
        "timezone": 8,
        "tzoffset": 0,
        "interval": "2.5s",
        "intervalMs": 2500,
        "maxDataPoints": 5,
        "mock": 0,
        "range": {
          "from": `${startTime}T16:00:00.000Z`,
          "to": `${this.cdata.category[this.cdata.category.length - 1]}T16:00:00.000Z`,
          "raw": {"from": "now-5m", "to": "now"}
        },
        "targets": [{
          "target": "对标值",
          "apmOrgId": 1,
          "building": "海辰储能电力",
          "displayName": "对标值",
          "formulaType": 121,
          "formulaUnit": "day",
          "param1": {"key": "标煤/标電/標碳", "name": "换算方式", "type": 1, "value": 1},
          "param2": {"key": "總量/面積/人均", "name": "折算方式", "type": 2, "value": 0},
          "param3": {"key": "對標值", "name": "對標值", "type": 3, "value": 1},
          "queryType": "bems_multi",
          "range": "day",
          "resourceId": "2785",
          "subitemCode": ["01000"],
          "nodeType": "park"
        }, {
          "target": "电本期",
          "building": "海辰储能电力",
          "apmOrgId": 1,
          "queryType": "bems_multi",
          "resourceId": "2785",
          "subitemCode": "01000",
          "formulaUnit": "day",
          "range": "day",
          "formulaType": 173,
          "param1": {"type": 1, "name": "换算方式", "key": "标煤/标電/標碳", "value": 1},
          "param2": {"type": 2, "name": "折算方式", "key": "總量/面積/人均", "value": 0},
          "displayName": "电",
          "nodeType": "park"
        }, {
          "target": "电同比",
          "building": "海辰储能电力",
          "apmOrgId": 1,
          "queryType": "bems_multi",
          "resourceId": "2785",
          "subitemCode": "01000",
          "formulaUnit": "day",
          "range": "day",
          "formulaType": 173,
          "param1": {"type": 1, "name": "换算方式", "key": "标煤/标電/標碳", "value": 1},
          "param2": {"type": 2, "name": "折算方式", "key": "總量/面積/人均", "value": 0},
          "param4": {"type": 0, "name": "對比方式", "key": "同比", "value": 1},
          "displayName": "电同比",
          "nodeType": "park"
        }, {
          "target": "电环比",
          "building": "海辰储能电力",
          "apmOrgId": 1,
          "queryType": "bems_multi",
          "resourceId": "2785",
          "subitemCode": "01000",
          "formulaUnit": "day",
          "range": "day",
          "formulaType": 173,
          "param1": {"type": 1, "name": "换算方式", "key": "标煤/标電/標碳", "value": 1},
          "param2": {"type": 2, "name": "折算方式", "key": "總量/面積/人均", "value": 0},
          "param4": {"type": 0, "name": "對比方式", "key": "环比", "value": 2},
          "displayName": "电环比",
          "nodeType": "park"
        }]
      }
      getsimplejson(body).then(res => {
        console.log('res=>', res)
        res.map(v => {
          console.log('v=>', v)
          if (v.target == '电本期') {
            this.cdata.lineData = v.datapoints.map(v1 => v1[0])
          }
          if (v.target == '电环比') {
            this.cdata.barData = v.datapoints.map(v1 => v1[0])
          }
        })
      })
      console.log('this.cdata=>', this.cdata)
      // for (let i = 0; i < this.cdata.barData.length - 1; i++) {
      //   let rate = this.cdata.barData[i] / this.cdata.lineData[i];
      //   this.cdata.rateData.push(rate.toFixed(2));
      // }
    },
    get_date(num) {
      let dateArray = []
      //获取今天日期
      let myDate = new Date()
      let today = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + "-" + myDate.getDate();
      myDate.setDate(myDate.getDate() - num)
      let dateTemp;  // 临时日期数据
      let flag = 1;
      for (let i = 0; i < num; i++) {
        dateTemp = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + "-" + myDate.getDate()
        dateArray.push({
          date: dateTemp
        })
        myDate.setDate(myDate.getDate() + flag);
      }
      dateArray.push({
        date: today
      })
      let arr = []
      let newArr = []
      dateArray.forEach(item => {
        arr.push(item.date.split('-'))
      })
      for (let i = 0; i < arr.length; i++) {
        if (arr[i][1] < 10) {
          arr[i][1] = "0" + arr[i][1]
        }
        if (arr[i][2] < 10) {
          arr[i][2] = "0" + arr[i][2]
        }
      }
      for (let j = 0; j < arr.length; j++) {
        newArr.push(arr[j].join("-"))
      }
      // 当前日期
      let nowDate = newArr[newArr.length - 1]
      // 30天前日期
      let previousDate = newArr[0]
      // 日期区间数组
      console.log(nowDate)
      console.log(previousDate)
      console.log(newArr)
      return newArr
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
